package com.fandou.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.fandou.base.R;

public class NumberStepController extends LinearLayout {

    private final static int MODE_DARK = 0;

    private final static int MODE_LIGHT = 1;


    /**
     * 默认的最小间隔
     */
    public static final int DEFAULT_MIN_INTERVAL = 5;

    /**
     * 默认的最大间隔
     */
    public static final int DEFAULT_MAX_INTERVAL = 99;

    private TextView tv_number;
    private TextView tv_unit;
    private ImageView iv_add;
    private ImageView iv_dec;

    private int number;
    private int minNumber;
    private int maxNumber;

    /**
     * 0: dark
     * 1：light
     * default is dark
     */
    private int mode = MODE_DARK;


    private OnNumberChangeListener onNumberChangeListener;

    public NumberStepController(Context context) {
        this(context, null);
    }

    public NumberStepController(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberStepController(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initParams(context, attrs);
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.number_step_controller_layout, this);

        tv_number = findViewById(R.id.number_content);
        tv_unit = findViewById(R.id.number_unit);
        iv_add = findViewById(R.id.number_add_btn);
        iv_dec = findViewById(R.id.number_dec_btn);

        iv_add.setOnClickListener(v -> add());
        iv_dec.setOnClickListener(v -> dec());
    }

    private void initParams(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberStepController);
        int btnSize = typedArray.getDimensionPixelSize(R.styleable.NumberStepController_number_btn_size,
                getResources().getDimensionPixelSize(R.dimen.land_x40));
        int componentInterval = typedArray.getDimensionPixelSize(R.styleable.NumberStepController_number_component_interval,
                getResources().getDimensionPixelSize(R.dimen.land_x28));
        minNumber = typedArray.getInt(R.styleable.NumberStepController_number_min, DEFAULT_MIN_INTERVAL);
        maxNumber = typedArray.getInt(R.styleable.NumberStepController_number_max, DEFAULT_MAX_INTERVAL);
        number = typedArray.getInt(R.styleable.NumberStepController_number_progress, minNumber);
        String unit = typedArray.getString(R.styleable.NumberStepController_number_unit_text);
        mode = typedArray.getInt(R.styleable.NumberStepController_number_mode, MODE_DARK);

        if (mode==MODE_DARK){
            iv_add.setImageResource(R.mipmap.btn_step_add);
            iv_dec.setImageResource(R.mipmap.btn_step_dec);
            tv_number.setTextColor(getResources().getColor(R.color.black_333333));
            tv_unit.setTextColor(getResources().getColor(R.color.black_333333));
        }else{
            iv_add.setImageResource(R.mipmap.btn_step_add_light);
            iv_dec.setImageResource(R.mipmap.btn_step_dec_light);
            tv_number.setTextColor(getResources().getColor(R.color.white));
            tv_unit.setTextColor(getResources().getColor(R.color.white));
        }

        //btn size
        resetSize(iv_add, btnSize);
        resetSize(iv_dec, btnSize);

        //interval
        resetInterval(componentInterval);


        if (TextUtils.isEmpty(unit)) {
            tv_unit.setVisibility(GONE);
        } else {
            tv_unit.setVisibility(VISIBLE);
            tv_unit.setText(unit);
        }

        if (number < minNumber) {
            number = minNumber;
        } else if (number > maxNumber) {
            number = maxNumber;
        } else {
        }
        tv_number.setText(parseNumber(number));
    }

    public void add() {
        int num = number < maxNumber ? number + 1 : number;
        setNumber(num, true);
    }

    public void dec() {
        int num = number > minNumber ? number - 1 : number;
        setNumber(num, true);
    }

    public void setNumber(int number) {
        setNumber(number, false);
    }

    public int getMinNumber(){
        return minNumber;
    }

    public void setMinNumber(int minNumber) {
        this.minNumber = minNumber;
    }

    public void setMaxNumber(int maxNumber) {
        this.maxNumber = maxNumber;
    }

    public int getNumber() {
        return number;
    }

    private void setNumber(int number, boolean isManual) {
        this.number = number;
        tv_number.setText(parseNumber(number));

        if (onNumberChangeListener != null) {
            onNumberChangeListener.onChange(this.number, isManual);
        }
    }

    private String parseNumber(int number) {
        return number < 10 ? "0" + number : Integer.toString(number);
    }

    private void resetSize(View targetView, int size) {
        LayoutParams layoutParams = (LayoutParams) targetView.getLayoutParams();
        layoutParams.width = size;
        layoutParams.height = size;
        targetView.setLayoutParams(layoutParams);
    }

    private void resetInterval(int interval) {
        LayoutParams numLayoutParams = (LayoutParams) tv_number.getLayoutParams();
        numLayoutParams.leftMargin = interval;
        numLayoutParams.rightMargin = interval;
        tv_number.setLayoutParams(numLayoutParams);

        LayoutParams unitLayoutParams = (LayoutParams) tv_unit.getLayoutParams();
        unitLayoutParams.leftMargin = interval;
        tv_unit.setLayoutParams(unitLayoutParams);
    }

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    public interface OnNumberChangeListener {
        void onChange(int number, boolean isManual);
    }

    public static class NumberStepClickProxy {
        public void add() {

        }

        public void dec() {

        }
    }
}
